package com.yunc.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunc.base.controller.CommonController;
import com.yunc.common.aspect.BaseLog;
import com.yunc.common.utils.R;
import com.yunc.modules.sys.entity.SysDept;
import com.yunc.modules.sys.service.ISysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理
 *
 * @author congcong tao
 * @create 2018/11/20 下午10:21
 */
@RestController
@RequestMapping("sysDept")
public class SysDeptController extends CommonController {

    private static final Logger logger = LogManager.getLogger(SysDeptController.class);

    @Autowired
    private ISysDeptService sysDeptService;

    @GetMapping("/list")
    public R list(SysDept sysDept){
        QueryWrapper<SysDept> ew = new QueryWrapper<SysDept>()
                .orderByAsc("order_num");
        if (StringUtils.isNotEmpty(sysDept.getName())) {
            ew.like("name", sysDept.getName());
        }
        List<SysDept> sysDeptList = sysDeptService.list(ew);
        return R.ok().put("data", sysDeptList);
    }

    @GetMapping("/queryDeptList")
    public List<SysDept> queryDeptList(SysDept sysDept){
        List<SysDept> sysDeptList = sysDeptService.queryDeptList(sysDept);
        return sysDeptList;
    }

    @GetMapping("/queryDeptListR")
    public R queryDeptListR(SysDept sysDept){
        List<SysDept> sysDeptList = sysDeptService.queryDeptList(sysDept);
        return R.ok().put("sysDeptList", sysDeptList);
    }

    @GetMapping("/info/{deptId}")
    public R info(@PathVariable String deptId){
        SysDept dept = sysDeptService.getById(deptId);
        return R.ok().put("sysDept", dept);
    }

    /**
     * 保存
     */
    @BaseLog("部门管理")
    @PostMapping("/save")
    @CacheEvict(value={"user-dept-tree-key"}, allEntries=true)
    public R save(@RequestBody SysDept dept){
        return sysDeptService.saveDept(dept);
    }

    /**
     * 修改
     */
    @BaseLog("部门管理")
    @PutMapping(value = "/update")
    @CacheEvict(value={"user-dept-tree-key"}, allEntries=true)
    public R update(@RequestBody SysDept dept){
        return sysDeptService.updateDept(dept);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{deptId}")
    @RequiresPermissions("sys:dept:delete")
    @CacheEvict(value={"user-dept-tree-key"}, allEntries=true)
    public R delete(@PathVariable Long deptId){
        SysDept record = new SysDept();
        record.setParentId(deptId);
        QueryWrapper<SysDept> ew = new QueryWrapper<>(record);
        int count = sysDeptService.count(ew);
        if (count > 0) {
            return R.error("存在子部门，不允许删除");
        }
        boolean flag = sysDeptService.removeById(deptId);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
